XML Freeform

Let’s look again at our employee XML document that we have been using.

<?xml version="1.0"?>
<employees>
    <employee id="12345-67890">
        <name>
            <first>John</first>
            <last>Doe</last>
        </name>
        <position>Programmer</position>
        <address>
            <street>123 Main Street</street>
            <city>Anywhere</city>
            <state>CA</state>
            <zip>92000</zip>
        </address>
        <phone>
            <main>(714) 555-1000</main>
            <fax>(714) 555-1001</fax>
        </phone>
    </employee>
</employees>

File: Chapter 4\Employees.xml

This document is entirely free form and can contain any amount of data that we choose.  We could for example add another employee to this document with totally different elements and it would work just fine because we have not stipulated any rules regarding the content.

<?xml version="1.0"?>
<employees>
    <!-- Employee 1 -->
    <employee id="12345-67890">
        <name>
            <first>John</first>
            <last>Doe</last>
        </name>
        <position>Programmer</position>
        <address>
            <street>123 Main Street</street>
            <city>Anywhere</city>
            <state>CA</state>
      	<zip>92000</zip>
        </address>
   	  <phone>
            <main>(714) 555-1000</main>
      	<fax>(714) 555-1001</fax>
   	  </phone>
	</employee>

	  <!-- Employee 2 -->
	  <employee>
		<name>Jane Doe</name>
			<title>Systems Analyst</title>
   		<phone>(714) 555-1002</phone>
	  </employee>
</employees>

File: Chapter 4\NewEmployee.xml

Notice the elements associated with the second employee.  There are only three: <name>, <title> and <phone>.  This is in stark contrast with the elements associated with the first employee.  

If we were to look at the logical structure of this document, it would look like this:

File: Chapter 4\NewEmployee.xml Logical Diagram 

There are five distinct differences between employee 1 (John Doe) and employee 2 (Jane Doe).  

In the first employee, the <employee> tag has an “id” attribute, the second <employee> tag does not.

In the first employee, <name> has two children: <first> and <last>.  In the second employee, <name> contains the entire name of the employee and does not have any child elements associated with it.

In the first employee, a <position> element specifies the role that the employee has in the company.  In the second employee, the <position> element has been changed to <title>.

In the first employee, an <address> element contains four child elements (<street>, <city>, <state> and <zip>) that in turn contain the full address of the employee.  The second employee has no address at all.

Finally, the <phone> element in employee 1 contains child elements specifying phone numbers by type.  In employee 2, the <phone> element has no children and specifies a single phone number without a type.

The real question is, “is this legal in XML terms?”  The answer (surprising to some) is “yes”!  Notice how both XML Notepad and Microsoft Internet Explorer treat this document.

File: Chapter 4\NewEmployee.xml in XML Notepad.

File: Chapter 4\NewEmployee.xml

XML is extremely lenient when there are no rules applied to it.  You can add elements to any part of the document, establish parent/child/sibling relationships at will and create an awful mess.  This is not the goal of XML.

XML by its nature wants to be structured.  Without structure, XML is no better than a flat text file that contains data.